package com.oristand.printer.address.util;

import com.zebra.sdk.printer.discovery.DiscoveredPrinterDriver;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.control.*;
import org.apache.commons.lang3.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * @author chenkailiang
 * @date 2020/9/24
 */
public class SavePrinterConfigurationWork extends Task<Void> {

    private ChoiceBox connectionType;

    private ChoiceBox printerListChoiceBox;

    private TextField ipAddressTextField;

    private TextField portTextField;

    private Button savePrinterButton;

    private Button cancelSavePrinterButton;
    private Button editPrinterButton;


    public SavePrinterConfigurationWork(ChoiceBox connectionType, ChoiceBox printerListChoiceBox,
                                        TextField ipAddressTextField,TextField portTextField,
                                        Button savePrinterButton,Button cancelSavePrinterButton,Button editPrinterButton){
        this.ipAddressTextField = ipAddressTextField;
        this.portTextField = portTextField;
        this.connectionType = connectionType;
        this.printerListChoiceBox = printerListChoiceBox;
        this.savePrinterButton = savePrinterButton;
        this.cancelSavePrinterButton = cancelSavePrinterButton;
        this.editPrinterButton = editPrinterButton;
    }


    @Override
    @SuppressWarnings("unchecked")
    protected Void call()  {
        try {
            TimeUnit.MILLISECONDS.sleep(1000L);
            SelectionModel model  = connectionType.getSelectionModel();
            String printerConnectionType = (String)model.getSelectedItem();
            String printerName;
            String ipAddress;
            String port;
            String sql;
            if(Constants.CONNECTION_TYPE_USB.equals(printerConnectionType)){
                SelectionModel printer  = printerListChoiceBox.getSelectionModel();
                DiscoveredPrinterDriver printerDriver = (DiscoveredPrinterDriver)printer.getSelectedItem();
                if(printerDriver == null || printerDriver.printerName.isEmpty()){
                    Platform.runLater(()->CommonMethodUtil.tipAlert(Constants.PRINTER_NOT_CHOOSE,null));
                    return null;
                }
                printerName = printerDriver.printerName;
                if(StringUtils.isBlank(printerName)){
                    return null;
                }
                sql  = "insert into printerConfiguration (connectionType,printerName)values ('"+printerConnectionType+"','"
                        +printerName+"')";
            }else{
                ipAddress = ipAddressTextField.getText();
                port = portTextField.getText();
                sql  = "insert into printerConfiguration (connectionType,ipAddress,port)values ('"+printerConnectionType+"','"
                        +ipAddress+"','"+port+"')";
            }
            //清空打印机配置表，只保留新的一个配置
            String clearSql = "delete from printerConfiguration";
            DbUtil.printerConfigurationDbUtil.executeUpdate(clearSql);
            DbUtil.printerConfigurationDbUtil.executeUpdate(sql);

            boolean flag = PrinterUtil.checkPrinterStatus(printerListChoiceBox.getItems());
            setButtonVisible(flag);
        } catch (Exception e) {
            Platform.runLater(()->CommonMethodUtil.tipAlert(Constants.DATABASE_ERROR,e));
        }
        return null;
    }

    /**
     * 设置按钮显示与隐藏
     * @param flag flag
     */
    private void setButtonVisible(boolean flag){
        savePrinterButton.setVisible(!flag);
        cancelSavePrinterButton.setVisible(!flag);
        editPrinterButton.setVisible(flag);
    }




}
